ARG NODE_JS_VERSION=${NODE_JS_VERSION:-18}
FROM node:${NODE_JS_VERSION}

ARG BUILDER_UID=1000
ARG BUILDER_GID=1000
ENV BUILDER_USER elastic
ENV BUILDER_GROUP elastic

# install zip util
RUN apt-get clean -y && \
    apt-get update -y && \
    apt-get install -y zip

# Set user permissions and directory
RUN (id -g ${BUILDER_GID} || groupadd --system -g ${BUILDER_GID} ${BUILDER_GROUP}) \
    && (id -u ${BUILDER_UID} || useradd --system --shell /bin/bash -u ${BUILDER_UID} -g ${BUILDER_GID} -m elastic) \
    && mkdir -p /usr/src/elasticsearch-js \
    && chown -R ${BUILDER_UID}:${BUILDER_GID} /usr/src/

WORKDIR /usr/src/elasticsearch-js

# run remainder of commands as non-root user
USER ${BUILDER_UID}:${BUILDER_GID}

# install dependencies
COPY package.json .
RUN npm install --production=false

# copy project files
COPY . .
